home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_02 / saks / testpart.cpp < prev   
Encoding:
C/C++ Source or Header  |  1993-12-14  |  1.4 KB  |  41 lines

  1. Listing 6 - a test program for the hierarchy in Listing 5.
  2.  
  3. int main()
  4.     {
  5.     C c;
  6.     D d;
  7.  
  8.     B *pb = &c;         // ok, &c is a C * which is a B *
  9.     pb->f(1);           // calls C::f(int)
  10.     pb->f(2L);          // calls B::f(long)
  11.     pb->f("hello");     // calls B::f(char *)
  12.  
  13.     d.f(1);             // calls D::f(long)
  14.                         // C::f(int) is hidden
  15.                         // but 1 promotes to long
  16.     d.C::f(1);          // calls C::f(int)
  17.     d.B::f(1);          // calls B::f(int)
  18.     d.f(1L);            // calls D::f(long)
  19.     d.C::f(1L);         // calls C::f(int)
  20.                         // B::f(long) is hidden
  21.                         // but 1L converts to int
  22.     d.B::f(1L);         // calls B::f(long)
  23.  
  24.     C *pc = &d;         // ok, &d is a D * which is a C *
  25.     pc->f(1);           // calls C::f(int)
  26.     pc->f(1L);          // calls C::f(int)
  27.                         // B::f(long) is hidden
  28.                         // but 1L converts to int
  29.     pc->B::f(1L);       // calls B::f(long)
  30.     pc->f("hello");     // error!  f(char *) is hidden
  31.     pc->B::f("hello");  // calls B::f(char *)
  32.  
  33.     B &rb = *pc;        // ok, *pc is a C which is a B
  34.     rb.f(1);            // calls C::f(int)
  35.     rb.f(2L);           // calls D::f(long)
  36.     rb.B::f(2L);        // calls B::f(long)
  37.     rb.f("hello");      // calls B::f(char *)
  38.  
  39.     return 0;
  40.     }
  41.